require("TSLib")
local warehouse = require("warehouse")
local toucher = require("toucher")
local logger = require("logger")

local M ={
	row = 4,
	column = 5
}

function M.open_warehouse(...)
	if (isColor( 775,  554, 0xd1494a, 85)) then
		toucher.click(775,555)
		mSleep(1000)
	else
		logger.warn("knapsack maybe not open.")
		return false
	end
	return true
end

function M.is_open(...)
	if false == (isColor( 952,  553, 0x3f837c, 85)) or
	   false == (isColor( 775,  554, 0xd1494a, 85))
	then
		return false
	end
	return true
end

function M.grid(row,column)
	local knapsack_column = {700, 780, 860, 940, 1020}
	local knapsack_row = {170, 240, 320, 370}
	return knapsack_column[column], knapsack_row[row]
end

-- 整理背包
function M.tidy(...)
	if (isColor( 953,  553, 0x3f837c, 85)) then
		toucher.click(953, 553)
		return true
	else
		logger.warn("the knapsack not open.")
		return false
	end
end

-- 存储到仓库
function M.store_in(row, column)
	if not (warehouse.is_open()) then
		M.open_warehouse()
		mSleep(1000)
	end
	x,y = M.grid(row,column)
	toucher.double_click(x,y)
end


-- 存储到仓库
function M.store_all_in()
	for row = 1, M.row, 1 do
		for column = 1, M.column, 1 do
			store_in(row,column)
			mSleep(1000)
		end
	end
	M.tidy()
	mSleep(1000)
end


function main(...)
	init(1)
	toast("knapsack")
	--M.store_in_warehouse()
	nLog(M.store_in(2,2))
	
end


--main()



return M